﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace BTCairo
{
    public partial class Form1 : Form
    {
        List<Shape> listShape = new List<Shape>();
        List<Model> listModelShape = new List<Model>();
        //List<Shape> listShapeUndo = new List<Shape>();
        InfoGraphic info = new InfoGraphic(true, 2, 255, 0, 0, 0, 255, 0);
        //CommonGraphics comGra = null;
        System.Drawing.Graphics g = null;
        int x1, y1, x2, y2;
        int type;//1:line; 2:rectangle; 3:ellipse; 4:circle
        int diagram;//1:flowchart, 2:dataflowdiagram; 3:activitydiagram
        bool shadow, highligh, border;
        bool fill;
        public Form1()
        {
            InitializeComponent();
        }
        int a = 100;
        //private void gh(object sender, PaintEventArgs e)
        //{
            ////Create Graphic
            //CommonGraphics comGra = new GDIPlus(e.Graphics, info);
            ////CommonGraphics comGra = new Cairo(e.Graphics, info);

            //this.MyPaint();

            //switch (type)
            //{
            //    case 1://Line
            //        {
            //            Shape line = new Line(comGra, x1, y1, x2, y2);
            //            line.Draw();
            //            listShape.Add(line);
            //        }
            //        break;
            //    case 2://Rectangle
            //        {
            //            Shape rect = new Rect(comGra, x1, y1, x2 - x1, y2 - y1);
            //            rect.Draw();
            //            listShape.Add(rect);
            //        }
            //        break;
            //    case 3://Ellipse
            //        {
            //            Shape ellipse = new Ellipse(comGra, x1, y1, x2 - x1, y2 - y1);
            //            ellipse.Draw();
            //            listShape.Add(ellipse);
            //        }
            //        break;
            //    case 4://Circle
            //        {
            //            Shape circle = new Circle(comGra, x1, y1, x2 - x1);
            //            circle.Draw();
            //            listShape.Add(circle);
            //        }
            //        break;
            //    default:
            //        break;
            //}

            //if (type != 0)
            //{
            //    foreach (Shape shape in listShape)
            //    {
            //        shape.Draw();
            //    }
            //    type = 0;
            //}




            ////Create a Shape
            ////Shape myLine = new Line(comGra, a++, 100, a++, 600);
            ////Invalidate();
            ////myLine.shadow = new Shadow();
            ////listShape.Add(myLine);
            ////myLine.Draw();
            //foreach (Shape shape in listShape)
            //{
            //    shape.Draw();
            //}

            //Shape rect = new Rect(comGra, 100, 100, 200, 200);
            //rect.shadow = new Shadow();
            ////rect.highlight = new Highlight();
            ////rect.border = new Border();
            //rect.Draw();
            //listShape.Add(rect);

            //Shape cirle = new Circle(comGra, 50, 50, 100);
            //cirle.highlight = new Highlight();
            //cirle.Draw();
            //listShape.Add(cirle);


            //StartModel start = new StartModel(comGra, new FlowChart(), 200, 150, 150, 100);
            ////start.border = new Border();
            //start.shadow = new Shadow();
            //start.Draw();
            //listShape.Add(start);

            //start.AbstractFactory = new DataFlowDiagram();
            //start.Draw();

            //start.AbstractFactory = new ActivityDiagram();
            //start.Draw();

            //Shape rect2 = new Rect(comGra, 250, 200, 200, 200);
            //rect2.Draw();
            //listShape.Add(rect2);




            //SaveType pngType = new SVGType(listShape, @"E:\out", info);
            //pngType.Save();
        //}


        private void MyPaint()
        {
            CommonGraphics comGra = null;
            this.g = panel1.CreateGraphics();
            info.fill = fill;
            comGra = new GDIPlus(this.g, info);
            this.g.Clear(Color.White);
            Shape shape = null;
            Model modelShape = null;
            AbstractFactory factory = new FlowChart();
            switch (diagram)
            {
                case 1:
                    {
                        factory = new FlowChart();
                    }
                    break;
                case 2:
                    {
                        factory = new DataFlowDiagram();
                    }
                    break;
                case 3:
                    {
                        factory = new ActivityDiagram();
                    }
                    break;
                default: break;
            }
            switch (type)
            {
                case 1://Line
                    {
                        shape = new Line(comGra, x1, y1, x2, y2);
                    }
                    break;
                case 2://Rectangle
                    {
                        shape = new Rect(comGra, x1, y1, x2 - x1, y2 - y1);
                    }
                    break;
                case 3://Ellipse
                    {
                        shape = new Ellipse(comGra, x1, y1, x2 - x1, y2 - y1);
                    }
                    break;
                case 4://Circle
                    {
                        shape = new Circle(comGra, x1, y1, x2 - x1);
                    }
                    break;
                case 5://Start Model
                    {
                        modelShape = new StartModel(comGra, factory, x1, y1, x2, y2);
                    }
                    break;
                default:
                    break;
            }
            //Shape
            if (shape != null)
            {
                if (shadow)
                {
                    shape.shadow = new Shadow();
                }
                if (highligh)
                {
                    shape.highlight = new Highlight();
                }
                if (border)
                {
                    shape.border = new Border();
                }

                //Add to list
                listShape.Add(shape);
            }
            //Model shape
            if (modelShape != null)
            {
                if (shadow)
                {
                    modelShape.shadow = new Shadow();
                }
                if (highligh)
                {
                    modelShape.highlight = new Highlight();
                }
                if (border)
                {
                    modelShape.border = new Border();
                }

                //Add to list
                listModelShape.Add(modelShape);
            }



            //Draw all
            int nShape = listShape.Count;
            foreach (Shape obj in listShape)
            {
                obj.Draw();
            }

            //Draw Model Shape
            int nModelShape = listModelShape.Count;
            foreach (Model obj in listModelShape)
            {
                obj.AbstractFactory = factory;
                obj.Draw();
            }
            //Invalidate();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //this.g = panel1.CreateGraphics();
            //this.comGra = new GDIPlus(this.g, info);
        }

        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {

        }

        //private void Form1_MouseDown(object sender, MouseEventArgs e)
        //{
        //    x1 = e.X;
        //    y1 = e.Y;
        //}

        //private void Form1_MouseUp(object sender, MouseEventArgs e)
        //{
        //    x2 = e.X;
        //    y2 = e.Y;

        //    //switch (key)
        //    //{
        //    //    case 1: type = 1; break;
        //    //    case 2: type = 2; break;
        //    //    case 3: type = 3; break;
        //    //    case 4: type = 4; break;
        //    //    default: break;
        //    //}

        //    CommonGraphics comGra = new GDIPlus(panel1.CreateGraphics(), info);
        //    Shape shape = null;
        //    switch (type)
        //    {
        //        case 1://Line
        //            {
        //                shape = new Line(comGra, x1, y1, x2, y2);
        //            }
        //            break;
        //        case 2://Rectangle
        //            {
        //                shape = new Rect(comGra, x1, y1, x2 - x1, y2 - y1);
        //            }
        //            break;
        //        case 3://Ellipse
        //            {
        //                shape = new Ellipse(comGra, x1, y1, x2 - x1, y2 - y1);
        //            }
        //            break;
        //        case 4://Circle
        //            {
        //                shape = new Circle(comGra, x1, y1, x2 - x1);
        //            }
        //            break;
        //        default:
        //            break;
        //    }
        //    if (shadow)
        //    {
        //        shape.shadow = new Shadow();
        //    }
        //    if (highligh)
        //    {
        //        shape.highlight = new Highlight();
        //    }
        //    if (border)
        //    {
        //        shape.border = new Border();
        //    }


        //    listShape.Add(shape);
        //    //Draw all
        //    foreach (Shape obj in listShape)
        //    {
        //        obj.Draw();
        //    }

        //    //Invalidate();
        //}

        private void btnLine_Click(object sender, EventArgs e)
        {
            type = 1;
        }

        private void btnEllipse_Click(object sender, EventArgs e)
        {
            type = 3;
        }

        private void btnRectangle_Click(object sender, EventArgs e)
        {
            type = 2;
        }

        private void btnCircle_Click(object sender, EventArgs e)
        {
            type = 4;
        }
        private void btnmodelstart_Click(object sender, EventArgs e)
        {
            type = 5;
        }
        private void btnFlowChart_Click(object sender, EventArgs e)
        {
            diagram = 1;
            type = 0;
            this.MyPaint();
        }

        private void btnDataFlowDiagram_Click(object sender, EventArgs e)
        {
            diagram = 2;
            type = 0;
            this.MyPaint();
        }

        private void btnactivity_Click(object sender, EventArgs e)
        {
            diagram = 3;
            type = 0;
            this.MyPaint();
        }
        private void btnshadow_Click(object sender, EventArgs e)
        {
            shadow = !shadow;
        }

        private void btnhighlight_Click(object sender, EventArgs e)
        {
            highligh = !highligh;
        }

        private void btnborder_Click(object sender, EventArgs e)
        {
            border = !border;
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            x1 = e.X;
            y1 = e.Y;
        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            x2 = e.X;
            y2 = e.Y;

            this.MyPaint();
        }

        //private void btnundo_Click(object sender, EventArgs e)
        //{
        //    int countItem = listShape.Count;
        //    if (countItem > 0)
        //    {
        //        Shape item = listShape[countItem - 1];
        //        listShapeUndo.Add(item);
        //        listShape.RemoveAt(countItem - 1);
        //    };
        //    countItem = listShape.Count;
        //    //Invalidate(null, true);
        //    type = 0;
        //    this.MyPaint();
        //    //this.panel1_Paint(null, null);
        //    //Invalidate(null, true);
        //}

        //private void btnredo_Click(object sender, EventArgs e)
        //{
        //    int countItem = listShapeUndo.Count;
        //    if (countItem > 0)
        //    {
        //        Shape item = listShapeUndo[countItem - 1];
        //        listShape.Add(item);
        //        listShapeUndo.RemoveAt(countItem - 1);
        //    }

        //    //this.MyPaint();
        //    type = 0;
        //    this.MyPaint();
        //    //this.panel1_Paint(null, null);
        //}

        private void btnfill_Click(object sender, EventArgs e)
        {
            fill = !fill;
        }

        //private void panel1_Paint(object sender, PaintEventArgs e)
        //{
        //    this.MyPaint();
        //}

        

        
    }
}
